package com.lqh.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lqh.bean.Bill;
import com.lqh.bean.BillType;
import com.lqh.bean.ResultVO;
import com.lqh.service.BillService;
import com.lqh.service.BillTypeService;
import net.minidev.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @Author: liuqihong
 * @Description:
 * @Date Created in  2021-02-03 22:15
 * @Modified By:
 */
@RestController
@RequestMapping("/bill")
@RefreshScope //刷新配置
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private BillTypeService billTypeService;


    @RequestMapping("/list-page")
    public String ListPage(
        @RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int limit,
        Date date1,
        Date date2,
        @RequestParam(defaultValue = "0") String typeId
    ) {
        Bill bill = new Bill();
        bill.setDate1(date1);
        bill.setDate2(date2);
        bill.setTypeId(new Long(typeId));
        List<BillType> types = billTypeService.list();
        PageInfo<Bill> billPageInfo = billService.listPage(bill, page, limit);
        List<Bill> list = billPageInfo.getList();
        ResultVO resultVO = new ResultVO();
        resultVO.setData(list);
        resultVO.setCode(0);
        resultVO.setCount(billPageInfo.getTotal());
        String s = JSON.toJSONString(resultVO);
        return s;
    }

    /**
     * 查询
     * @param
     * @return
     */
    @GetMapping("/list")
    public String list(Bill b){
        List<Bill> list = billService.list(b);
        ResultVO resultVO = new ResultVO();
        resultVO.setData(list);
        resultVO.setCode(0);
        String s = JSON.toJSONString(resultVO);
        return s;
    }


    /**
     * 添加
     * @param b
     * @return
     */
    @RequestMapping("/add")
    public String add(Bill b,String callback){
        System.out.println(callback);
        System.out.println(b.getTitle());
        int add = billService.add(b);
        ResultVO resultV0 = new ResultVO();
        if (add > 0) {
            resultV0.setMsg("1");
            String jsonData = JSON.toJSONString(resultV0);
            return callback + "(" + jsonData + ")";
        }
        resultV0.setMsg("0");
        String jsonData = JSON.toJSONString(resultV0);
        System.out.println(jsonData);
        return callback + "(" + jsonData + ")";
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public String delete( Long id){
        int delete = billService.delete(id);
        return null;
    }

    /**
     * 通过id查找要修改的值
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/toUpdate/{id}")
    public Bill toUpdate(@PathVariable("id") Long id,Model model){
        List<BillType> types = billTypeService.list();
        model.addAttribute("types",types);

        Bill bill = billService.get(id);
        model.addAttribute("bill",bill);

        return bill;
    }

    /**
     * 修改
     * @param b
     * @return
     */
    @RequestMapping("/update")
    public String update(Bill b,String callback){
        System.out.println(b);
        int update = billService.update(b);
        ResultVO resultV0 = new ResultVO();
        if (update > 0) {
            resultV0.setMsg("1");
            String jsonData = JSON.toJSONString(resultV0);
            return callback + "(" + jsonData + ")";
        }
        resultV0.setMsg("0");
        String jsonData = JSON.toJSONString(resultV0);
        return callback + "(" + jsonData + ")";
    }


}
